package com.wangdao.wechatmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.wx.BaseCommentBO;
import com.wangdao.wechatmall.bean.bo.wx.BaseCommentListBO;
import com.wangdao.wechatmall.bean.bo.wx.WxCommentPostBO;
import com.wangdao.wechatmall.bean.pojo.Comment;
import com.wangdao.wechatmall.bean.pojo.CommentExample;
import com.wangdao.wechatmall.bean.pojo.User;
import com.wangdao.wechatmall.bean.vo.wx.*;
import com.wangdao.wechatmall.mapper.CommentMapper;
import com.wangdao.wechatmall.mapper.UserMapper;
import com.wangdao.wechatmall.util.DateTransformUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-12 16:22
 */
@Service
public class WXCommentServiceImpl implements WXCommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public CommentCountVO count(BaseCommentBO baseCommentBO) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(baseCommentBO.getValueId());
        criteria.andTypeEqualTo(baseCommentBO.getType());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentsPageInfo = new PageInfo<>(comments);
        long total = commentsPageInfo.getTotal();
        criteria.andHasPictureEqualTo(true);
        List<Comment> commentPics = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentsPicPageInfo = new PageInfo<>(commentPics);
        long total1 = commentsPicPageInfo.getTotal();
        CommentCountVO commentCountVO = new CommentCountVO();
        commentCountVO.setAllCount((int)total);
        commentCountVO.setHasPicCount((int)total1);
        return commentCountVO;
    }

    @Override
    public CommentListVO list(BaseCommentListBO baseCommentListBO) {
        PageHelper.startPage(baseCommentListBO.getPage(), baseCommentListBO.getSize());

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(baseCommentListBO.getValueId());
        List<Comment> comments;
        if (baseCommentListBO.getShowType() == 1) {
            criteria.andHasPictureEqualTo(true);
            comments = commentMapper.selectByExample(commentExample);
        } else {
            comments = commentMapper.selectByExample(commentExample);
        }



        return getCommentListVO(baseCommentListBO, comments);
    }

    @Override
    public WxCommentPostVO post(WxCommentPostBO wxCommentPostBO, Integer id) {
        Comment comment = new Comment();
        comment.setAddTime(DateTransformUtil.currentTime2Date(System.currentTimeMillis()));
        comment.setContent(wxCommentPostBO.getContent());
        comment.setDeleted(false);
        comment.setHasPicture(wxCommentPostBO.getHasPicture());
        comment.setPicUrls(wxCommentPostBO.getPicUrls());
        comment.setStar(wxCommentPostBO.getStar());
        comment.setType(wxCommentPostBO.getType());
        comment.setUpdateTime(comment.getAddTime());
        comment.setValueId(Integer.parseInt(wxCommentPostBO.getValueId()));
        comment.setUserId(id);
        commentMapper.insertSelective(comment);

        WxCommentPostVO wxCommentPostVO = new WxCommentPostVO();
        wxCommentPostVO.setAddTime(DateTransformUtil.dateStringFormat(comment.getAddTime()));
        wxCommentPostVO.setContent(comment.getContent());
        wxCommentPostVO.setHasPicture(comment.getContent());
        wxCommentPostVO.setId(comment.getId());
        wxCommentPostVO.setPicUrls(comment.getPicUrls());
        wxCommentPostVO.setStar(comment.getStar());
        wxCommentPostVO.setType(comment.getType());
        wxCommentPostVO.setUpdateTime(DateTransformUtil.dateStringFormat(comment.getUpdateTime()));
        wxCommentPostVO.setUserId(comment.getUserId());
        wxCommentPostVO.setValueId(comment.getValueId());

        return wxCommentPostVO;
    }

    private CommentListVO getCommentListVO(BaseCommentListBO baseCommentListBO, List<Comment> comments) {
        List<CommentDataVO> commentDataVOS = new ArrayList<>();
        for (Comment comment : comments) {
            CommentDataVO commentDataVO = new CommentDataVO();
            UserInfoVO userInfoVO = new UserInfoVO();
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            userInfoVO.setNickName(user.getNickname());
            userInfoVO.setAvatarUrl(user.getAvatar());
            commentDataVO.setAddTime(comment.getAddTime());
            commentDataVO.setContent(comment.getContent());
            List<String> strings = new ArrayList<>();
            String[] picUrls = comment.getPicUrls();
            for (String picUrl : picUrls) {
                strings.add(picUrl);
            }
            commentDataVO.setPicList(strings);
            commentDataVO.setUserInfo(userInfoVO);
            commentDataVOS.add(commentDataVO);
        }
        CommentListVO commentListVO = new CommentListVO();
        commentListVO.setCount(commentDataVOS.size());
        commentListVO.setCurrentPage(baseCommentListBO.getPage());
        commentListVO.setData(commentDataVOS);

        return commentListVO;
    }
}
